/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao;

import e.odbo.data.dao.I_DataLogic;
import e.odbo.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.I_BomProduct;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.DataWrite;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.table.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;
import com.openbravo.bean.ProductBom;
import com.openbravo.bean.Product;
import com.openbravo.data.loader.key.KeyBuilder;
import com.openbravo.pos.bean.ProductExt;

/**
 *
 * @author Administrator
 */
public class ProductBomDAO extends ModifiedLogDAO<ProductBom>{

    ProductDAO productDAO;

    public ProductBomDAO(I_Session s,ProductDAO productDAO) {
        super(s);
        this.productDAO=productDAO;
    }
    
    
     @Override
    public TableDefinition getTable() {
        return new TableDefinition(
                  "BOMS",
                  new Field[]{
                      new Field(I_BomProduct.ID,Datas.STRING, Formats.STRING),
                      new Field(I_BomProduct.PARENT,Datas.STRING, Formats.STRING,Product.class),
                      new Field(I_BomProduct.REFID,Datas.STRING, Formats.STRING),
                      new Field(I_BomProduct.UNITS,Datas.DOUBLE, Formats.DOUBLE),
                      new Field(I_BomProduct.LASTMODIFIED,Datas.TIMESTAMP, Formats.TIMESTAMP)
                      },
                  new int[]{0}
        );
    }

    public ProductBom readValues(DataRead dr,ProductBom bomProduct) throws BasicException {
            if(bomProduct==null)
            	bomProduct=new ProductBom();
            bomProduct.setId(dr.getString(1));
            bomProduct.setParentProductId(dr.getString(2));
            bomProduct.setProductID(dr.getString(3));
            bomProduct.setIncludeProduct(productDAO.find(KeyBuilder.getKey(dr.getString(3))));
            bomProduct.setUnits(dr.getDouble(4));
            bomProduct.setLastModified(dr.getTimestamp(5));
            return bomProduct;
    }

    @Override
    public Class transClass(Class in) {
        if(in==ProductExt.class)
            return Product.class;
        return super.transClass(in);
    }

    @Override
    public Class getSuportClass() {
        return ProductBom.class;
    }


    @Override
    public void writeInsertValues(DataWrite dp, ProductBom obj) throws BasicException {
             dp.setString(1,obj.getId());
             dp.setString(2, obj.getParentProductId());
             dp.setString(3, obj.getProductID());
             dp.setDouble(4, obj.getUnits());
             dp.setTimestamp(5,obj.getLastModified());
    }
}
